package com.thingsgrid.device.service.impl;

import com.thingsgrid.device.entity.Function;
import com.thingsgrid.device.service.IFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class FunctionServiceImpl implements IFunctionService {
	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 * 新增
	 * @param function
	 */
	@Override
	public void saveFunction(Function function) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = df.format(new Date());
		function.setCreateTime(format);
		mongoTemplate.save(function);
	}
	/**
	 * 删除
	 * @param functionId
	 */
	@Override
	public void removeFunction(String functionId) {
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("_id").is(functionId));
		mongoTemplate.remove(query,Function.class);
	}

	/**
	 * 更新
	 * @param function
	 */
	@Override
	public void updateFunction(Function function) {
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("fuctionType").is(function.getFuctionName()));
		Update update = new Update();
		update.set("fuctionType",function.getFuctionType());
		update.set("fuctionName",function.getFuctionName());
		update.set("identifier",function.getIdentifier());
		update.set("dataType",function.getDataType());
		update.set("dataDef",function.getDataDef());
		update.set("create_author",function.getCreate_author());
		update.set("createTime",function.getCreateTime());
		update.set("productId",function.getProductId());
		mongoTemplate.updateFirst(query,update,Function.class);
	}

	/**
	 * 查询所有
	 * @return
	 */
	@Override
	public List<Function> findAll() {
		return mongoTemplate.findAll(Function.class);
	}

	/**
	 * 根据id查找
	 * @param functionId
	 * @return
	 */
	@Override
	public Function findFunctionByName(String functionId) {
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("_id").is(functionId));
		Function one = mongoTemplate.findOne(query,Function.class);
		return one;
	}

	@Override
	public List<Function> findFunctionByProductId(String productId) {
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("productId").is(productId));
		List<Function> function = mongoTemplate.find(query,Function.class);
		return function;
	}

	/**
	 * 分页查找
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<Function> findAll(Integer pageIndex, Integer pageSize) {
		Query query = new Query();
		//Criteria criteria = new Criteria();
		Pageable pageable = new PageRequest(pageIndex-1,pageSize);
		query.with(pageable);
		return mongoTemplate.find(query,Function.class);
	}
}
